<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background-color: black;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div class="code">
        <canvas></canvas>
    </div>
    <script>
        // let canvas = document.createElement('canvas');
        // document.querySelector('.code').appendChild(canvas);

        var canvas = document.getElementsByTagName("canvas")[0];
        let ctx = canvas.getContext('2d')

        canvas.width = window.innerWidth
        canvas.height = window.innerHeight
        

        let code = 'IJFONSNDVJNSKJBFJSHJKNJSHF'
        code = code.split('')

        let fontSize = 16
        let space = canvas.width / fontSize

        let speed = []
        for (let i = 0; i < space; i++) {
            speed[i] = Math.floor(Math.random() * canvas.height);
        }
        console.log(speed);

        function draw() {
            ctx.fillStyle = 'rgba(0,0,0,0.05)'
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            ctx.fillStyle = '#0F0'
            ctx.font = fontSize + 'px arial'

            for (let i = 0; i < speed.length; i++) {
                let text = code[Math.floor(Math.random() * code.length)]
                ctx.fillText(text, i * fontSize, speed[i] * fontSize)
                if (speed[i] * fontSize >canvas.height && Math.random() > 0.975){
                    speed[i] = 0
                }
                speed[i]++
            }
        }
        setInterval(draw,30); // 每间隔30ms 执行一次draw方法
    </script>
</body>

</html>